package com.example.sxy_smartpark

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_base.path.RouterPath
import com.example.lib_common.view.adapter.ImageBannerAdapter
import com.example.lib_common.view.entity.BannerEntity

import com.example.lib_common.view.view.BaseMvvmActivity
import com.example.lib_image.glide.glide.GlideImgLoader
import com.example.module_main.MainActivity
import com.example.module_service.ServiceActivity
import com.example.sxy_smartpark.databinding.ActivitySplashBinding
import com.tencent.mmkv.MMKV
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerAdapter
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import com.youth.banner.transformer.AlphaPageTransformer

/**
 * 业务：1.倒计时：2.APP主图Banner切换：3.广告
 */
class SplashActivity : BaseMvvmActivity<ActivitySplashBinding>() {

    private var bannerData = mutableListOf<BannerEntity>()
    private lateinit var mBannerAdapter:ImageBannerAdapter
    private lateinit var CountDowmTime:CountDownTimer

    override fun getLayoutId(): Int =R.layout.activity_splash

    override fun initViews() {

        val isFrist = MMKV.defaultMMKV().decodeBool("isFristBoot", true)
        if (isFrist){
            MMKV.defaultMMKV().encode("isFristBoot",false)
            showAppBanner()
        }else{
            showAppAdv()
        }
//        //启动Activity
//        object :CountDownTimer(3000,1000){
//            override fun onTick(p0: Long) {
//                binding.djs.setText(""+p0/1000+"秒")
//            }
//
//            override fun onFinish() {
//               startActivity(Intent(this@SplashActivity,MainActivity::class.java))
//            }
//        }.start()
    }
    private fun showAppBanner(){
        binding.clAppBanner.visibility=View.VISIBLE//显示、、//View.GONE表示既不显示ui也不占位置
        binding.clAppAd.visibility=View.GONE//隐藏（不显示ui，但可以占位置）

        bannerData.run {
            add(BannerEntity(0, com.example.module_main.R.drawable.baseline_home_24_4,""))
            add(BannerEntity(1, com.example.module_main.R.drawable.baseline_room_service_24_4,""))
            add(BannerEntity(2, com.example.module_main.R.drawable.baseline_person_24_4,""))
        }
        mBannerAdapter=ImageBannerAdapter(bannerData)
        binding.appBanner.run {
            setAdapter(mBannerAdapter)
            //关联生命周期
            addBannerLifecycleObserver(this@SplashActivity)
            //设置指示器
            indicator=CircleIndicator(this@SplashActivity)
            //设置切换效果
            addPageTransformer(AlphaPageTransformer())
            //设置页面切换监听
            addOnPageChangeListener(object :OnPageChangeListener{
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int,
                ) {

                }

                override fun onPageSelected(position: Int) {
                   //判断Banner是否滑到最后一页
                    if (position==bannerData.size-1){
                        binding.tvStart.visibility=View.VISIBLE
                    }else{
                        binding.tvStart.visibility=View.GONE
                    }
                }

                override fun onPageScrollStateChanged(state: Int) {

                }
            })
        }
        //点击立即进入
        binding.tvStart.setOnClickListener {
            jumpMainPage()
        }


    }
    private fun showAppAdv(){
        binding.clAppBanner.visibility=View.GONE
        binding.clAppAd.visibility=View.VISIBLE

        //显示广告图片
        binding.ivAd.run {
            GlideImgLoader.loadResImage(this,R.drawable.guanggao_img,this)
        }
        //倒计时5秒
        CountDowmTime=object :CountDownTimer(3000,1000){
            override fun onTick(p0: Long) {
                binding.tvAdSkip.setText(""+p0/1000+"秒")
            }

            override fun onFinish() {
              this@SplashActivity.jumpMainPage()
            }
        }
        //启动倒计时
        CountDowmTime.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        CountDowmTime?.cancel()
    }



    override fun getActivityState(state: Bundle) {

    }

    override fun saveActivityState(outState: Bundle) {

    }

    //接收页面返回的数据
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
    }

    override fun loadData() {

    }

    override fun observeDataChang() {

    }

    private fun jumpMainPage(){
        ARouter.getInstance().build(RouterPath.PATH_MAIN_PAGE).navigation()
    }
}